package com.example.dragandview.http;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.dragandview.R;


import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static androidx.constraintlayout.widget.Constraints.TAG;

public class OkHttpActivity extends Activity {
    private TextView textView;
    private  String msg="";
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.okhttp_index);
        textView = findViewById(R.id.tv_okhttp);
    }
    //异步get
    public void asyncGet(View view) {
        //获取商城的分类信息
        String url = "https://www.sunofbeach.net/shop/api/discovery/categories";
        //1、创建client，理解为创建浏览器
        OkHttpClient okHttpClient = new OkHttpClient();
        new OkHttpClient.Builder().readTimeout(5,TimeUnit.SECONDS).build();
        OkHttpClient okHttpClient1 = new OkHttpClient().newBuilder().readTimeout(5, TimeUnit.SECONDS).build();
        //2、创建请求内容
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        //3、用浏览器创建调用任务
        Call call = okHttpClient.newCall(request);
        //4、执行任务
        call.enqueue(new Callback() {
            @Override
            public void onResponse( Call call,  Response response) throws IOException {
                System.out.println(response.body().string());
            }

            @Override
            public void onFailure( Call call,  IOException e) {
                System.out.println(e.toString());
            }

        });
    }
    //同步get
    public void syncGet(View view) {
        //获取商城的分类信息
        String url = "https://wanandroid.com/wxarticle/list/408/1/json";
        //1、创建client，理解为创建浏览器
        OkHttpClient okHttpClient = new OkHttpClient();
        //2、创建请求内容
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        //3、用浏览器创建调用任务
        final Call call = okHttpClient.newCall(request);
        //4、执行任务
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response response = call.execute();
                    msg = response.body().toString();
                    System.out.println("=================="+response.body().toString());
                } catch(IOException e) {
                    e.printStackTrace();
                    msg = e.toString();
                    System.out.println(e.toString());
                }
            }
        }).start();
        textView.setText(msg);
    }
//    //Post
//    public void doSearch(View view) {
//        String url = "https://www.sunofbeach.net/shop/api/search/1";
//        RequestBody requestBody = new FormBody.Builder()
//                .add("keyword","电脑")
//                .build();
//        OkHttpClient client = new OkHttpClient();
//        Request request = new Request.Builder()
//                .url(url)
//                .post(requestBody)
//                .build();
//        Call call = client.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call,@NotNull IOException e) {
//                Log.d(TAG,"onFailure -- > " + e.toString());
//            }
//
//            @Override
//            public void onResponse(@NotNull Call call,@NotNull Response response) throws IOException {
//                Log.d(TAG,"response json --> " + response.body().string());
//            }
//        });
//    }
//    //上传文件
//    public void postFile(View view) {
//        String url = "https://10.0.2.2:9102/file/upload";
//        OkHttpClient httpClient = new OkHttpClient.Builder().build();
//        File file = new File("/storage/emulated/0/Download/1.jpg");
//        MediaType mediaType = MediaType.parse("image/jpeg");
//        RequestBody fileBody = RequestBody.create(file,mediaType);
//        RequestBody requestBody = new MultipartBody.Builder()
//                .addFormDataPart("file",file.getName(),fileBody)
//                .build();
//        Request request = new Request.Builder().url(url).post(requestBody).build();
//        Call call = httpClient.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call,@NotNull IOException e) {
//                Log.d(TAG,"上传失败--> " + e.toString());
//            }
//
//            @Override
//            public void onResponse(@NotNull Call call,@NotNull Response response) throws IOException {
//                Log.d(TAG,"上传结果：" + response.body().string());
//            }
//        });
//    }
//    //上传多文件
//    public void postMultiFile(View view) {
//        String url = "https://10.0.2.2:9102/files/upload";
//        OkHttpClient httpClient = new OkHttpClient.Builder().build();
//        File fileOne = new File("/storage/emulated/0/Download/1.jpg");
//        File fileTwo = new File("/storage/emulated/0/Download/rBsADV3nxtKACoSfAAAPx8jyjF8169.png");
//        File fileThree = new File("/storage/emulated/0/Download/rBsADV2rEz-AIzSoAABi-6nfiqs456.png");
//        MediaType mediaType = MediaType.parse("image/jpeg");
//        RequestBody fileOneBody = RequestBody.create(fileOne,mediaType);
//        RequestBody fileTwoBody = RequestBody.create(fileTwo,mediaType);
//        RequestBody fileThreeBody = RequestBody.create(fileThree,mediaType);
//        RequestBody requestBody = new MultipartBody.Builder()
//                .addFormDataPart("files",fileOne.getName(),fileOneBody)
//                .addFormDataPart("files",fileTwo.getName(),fileTwoBody)
//                .addFormDataPart("files",fileThree.getName(),fileThreeBody)
//                .build();
//        Request request = new Request.Builder().url(url).post(requestBody).build();
//        Call call = httpClient.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call,@NotNull IOException e) {
//                Log.d(TAG,"多文件上传失败--> " + e.toString());
//            }
//
//            @Override
//            public void onResponse(@NotNull Call call,@NotNull Response response) throws IOException {
//                Log.d(TAG,"多文件上传结果：" + response.body().string());
//            }
//        });
//    }
}
